﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
using DG.Tweening;
public class Chanllenge : SceneMonoBase
{
    [HideInInspector]
    public bool IsPlayerIn;
    [HideInInspector]
    public bool IaActivated;
    [HideInInspector]
    public bool IsDisAppear;
    [HideInInspector]
    public int CurrentPlayerID;

    public override void OnEnable()
    {
        Alph = 0;
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alph);
        IsDisAppear = false;
        base.OnEnable();
    }
    public override void Awake()
    {
        base.Awake();
    }
    public override void Update()
    {
        if (!GameMgr.Get<ISceneMonoManager>().GetAllSceneMono()._sceneMonoDatas.ContainsKey(gameObject.name))
        {
            return;
        }
        if (!IsDisAppear)
        {
            CheckPlayerIsIn();
            CheckActivated();
            CheckPort();
        }
        base.Update();
    }

    float Alph = 0;
    float AlphPlus = 0;
    private void CheckActivated()
    {
        IaActivated = GameMgr.Get<IMapManager>().CheckClear(false);
        if (IaActivated)
        {
            if (Alph > 1f)
            {
                Alph = 1;
                AlphPlus = -0.025f;
            }
            if (Alph < 0.5f)
            {
                AlphPlus = 0.025f;
            }
            if (IsPlayerIn)
            {
                AlphPlus = 0.1f;
            }
            Alph += AlphPlus;
        }
        else
        {
            if (Alph < 0)
            {
                Alph = 0;
            }
            else
            {
                Alph -= Time.deltaTime;
            }
        }
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alph);
    }
    public void CheckPort()
    {
        if (IaActivated && IsPlayerIn && InputManager.Instance.Port(CurrentPlayerID))
        {
            IsDisAppear = true;
            foreach (SpawnObjData sb in GameMgr.Get<ISpawnManager>().GetSpawnObjDatas()._spawnData.Values)
            {
                if (sb.NotActivated)
                {
                    sb.NotActivated = false;
                }
            }
            int y = (int)(transform.position.y * 2);
            int x = (int)(transform.position.x * 2);

            var roomID = GameMgr.Get<IMapManager>().GetRoomID(y, x);
            GameMgr.Get<IMapManager>().GetCertainRoom(roomID).roomStatue = MapEditorBase.RoomStatue.Challenge;
            GetComponent<SpriteRenderer>().DOColor(new Color(0, 0, 0, 0), 0.5f).OnComplete(()=> {
                GameMgr.Get<ISceneMonoManager>().RemoveSceneMono(gameObject.name);
            });
        }
    }

    private void CheckPlayerIsIn()
    {
        IsPlayerIn = false;
        CurrentPlayerID = -1;
        foreach (var item in GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas)
        {
            if (GetDistance(item.PlayerMono.Position, transform.position) < 3)
            {
                IsPlayerIn = true;
                CurrentPlayerID = item.PlayerID;
            }
        }
    }
    private float GetDistance(Vector3 pos1, Vector3 pos2)
    {
        return (pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y);
    }

    public override void OnDrawGizmos()
    {
        Gizmos.color = new Color(0, 0, 0, 0.5f);
        base.OnDrawGizmos();
    }
}
